<script setup lang="ts">
import {
  getCompanyPage,
  updateCompany,
  getCompanyForm,
  addCompany,
} from "@/api/company";

import { CompanyPageVO, CompanyForm, CompanyQuery } from "@/api/company/types";

defineOptions({
  name: "Company",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);
const companyFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);

const queryParams = reactive<CompanyQuery>({
  pageNum: 1,
  pageSize: 10,
});

const companyList = ref<CompanyPageVO[]>();

const dialog = reactive<DialogOption>({
  visible: false,
});

const formData = reactive<CompanyForm>({});

const rules = reactive({
  companyName: [
    { required: true, message: "请输入公司名称", trigger: "blur" },
    { max: 64, message: "公司名称最长64个字符", trigger: "blur" },
  ],
  fullCompanyName: [
    { required: true, message: "请输入公司全称", trigger: "blur" },
    { max: 64, message: "公司全称最长255个字符", trigger: "blur" },
  ],
  companySuffix: [
    { required: true, message: "公司后缀不能为空", trigger: "blur" },
    { max: 64, message: "公司后缀最长32个字符", trigger: "blur" },
    {
      pattern: /^[A-Za-z0-9]{1,}$/,
      message: "公司后缀必须由字母、数字组成",
      trigger: "blur",
    },
  ],
  contactName: [
    { required: true, message: "请输入联系人", trigger: "blur" },
    { max: 32, message: "联系人最长32个字符", trigger: "blur" },
  ],
  contactMobile: [
    { required: true, message: "请输入联系人电话", trigger: "blur" },
    { max: 64, message: "联系人电话不能超过24个字符", trigger: "blur" },
  ],
  address: [
    { max: 255, message: "联系地址不能超过255个字符", trigger: "blur" },
  ],
  remark: [{ max: 255, message: "备注不能超过255个字符", trigger: "blur" }],
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  getCompanyPage(queryParams)
    .then(({ data }) => {
      companyList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 打开角色表单弹窗 */
function openDialog(companyId?: number) {
  dialog.visible = true;
  if (companyId) {
    dialog.title = "修公司";
    getCompanyForm(companyId).then(({ data }) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增公司";
  }
}

/** 角色保存提交 */
function handleSubmit() {
  companyFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const companyId = formData.id;
      if (companyId) {
        updateCompany(companyId, formData)
          .then(() => {
            ElMessage.success("修改成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        addCompany(formData)
          .then(() => {
            ElMessage.success("新增成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  companyFormRef.value.resetFields();
  companyFormRef.value.clearValidate();
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input
            v-model="queryParams.companyNameLike"
            placeholder="公司名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />搜索
          </el-button>
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header>
        <el-button type="success" @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="companyList"
        highlight-current-row
        border
      >
        <el-table-column label="公司名称" prop="companyName" min-width="100" />
        <el-table-column
          label="公司全称"
          prop="companyFullName"
          min-width="150"
        />
        <el-table-column label="公司后缀" prop="companySuffix" width="150" />
        <el-table-column label="联系人" prop="companyContact" width="200" />
        <el-table-column label="联系电话" prop="companyMobile" width="200" />
        <el-table-column label="修改者" prop="modifiedBy" width="200" />
        <el-table-column label="修改时间" prop="gmtModified" width="200" />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row.id)"
            >
              <i-ep-edit />编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 角色表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="700px"
      @close="closeDialog"
    >
      <el-form
        ref="companyFormRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="公司名称" prop="name">
          <el-input
            v-model="formData.companyName"
            placeholder="请输入公司名称"
          />
        </el-form-item>

        <el-form-item label="公司全称" prop="companyFullName">
          <el-input
            v-model="formData.companyFullName"
            placeholder="请输入公司全称"
          />
        </el-form-item>

        <el-form-item label="公司后缀" prop="companySuffix">
          <el-input
            v-model="formData.companyFullName"
            placeholder="请输入公司后缀"
          />
        </el-form-item>

        <el-form-item label="联系人" prop="contactName">
          <el-input v-model="formData.contactName" placeholder="请输入联系人" />
        </el-form-item>

        <el-form-item label="联系电话" prop="contactMobile">
          <el-input
            v-model="formData.contactMobile"
            placeholder="请输入联系电话"
          />
        </el-form-item>

        <el-form-item label="联系地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入联系地址" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
